#include <QtCore/QDebug>
#include "sudokugui.h"
#include "ui_sudokugui.h"


static QLineEdit *matriz[9][9];
static QLabel * av;

Sudokugui::Sudokugui(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Sudokugui)
{
    ui->setupUi(this);
    //QLineEdit *matriz[9][9];
    /*
    QRegExp rx("lineEdit_(.)_(.)");
    rx.indexIn("this is a test string");
    QString captured = rx.cap(1);*/

    for (int i = 0; i < 9 ; i++){
        for (int j = 0; j < 9; j++){
            QString strname = QString("lineEdit_%1_%2").arg(i).arg(j);
            matriz[i][j] = ui->frame->findChild<QLineEdit *>(strname);
            //qDebug() << matriz[i][j]->text();

            matriz[i][j]->setInputMask("D");
        }
    }
    ui->maquinas->setInputMask("D");
    av = ui->aviso;
    ui->aviso->hide();

    //qDebug() << ui->frame->findChild<QLineEdit *>("lineEdit_1");
    //system("mpirun -np 1 ../version1.2/sudoku");

}

Sudokugui::~Sudokugui()
{
    delete ui;
}

bool isRepeated(int fila, int columna, int valor){

    int firstf = fila - (fila%3);
    int firstc = columna - (columna%3);
    int vert = 3 - (fila%3);
    int horz = 3 - (columna%3);
    int parada;
    int stored_value;
     //qDebug() << "Fila:" << fila << " Columna:" << columna << " Valor:" << valor;
    //qDebug() << matriz[fila][columna]

    for (int i = firstf; i < firstf+3; i++){
        for (int j = firstc; j < firstc+3; j++){
            stored_value = 0;
            if (matriz[i][j]){

                if (matriz[i][j]->text() == ""){
                    continue;
                }else{
                    stored_value = (matriz[i][j]->text()).toInt();
                }
                if ((fila != i || columna != j) && (valor == stored_value)){
                    return true;
                }
            }
        }
    }

    parada = columna + horz;
    while ((parada%9) != firstc){
        if (matriz[firstf][parada%9]){
            if (valor == (matriz[firstf][parada%9]->text()).toInt()){
                return true;
            }
        }
        parada++;
    }

    parada = fila + vert;
    while (parada%9 != firstf){
        if (matriz[parada%9][firstc]){
            if (valor == (matriz[parada%9][firstc]->text()).toInt()){
                return true;
            }
        }
        parada++;
    }
    return false;
}

void QLineEdit::textChanged(const QString &str){

    if (av){
    av->hide();}
    if( !str.isEmpty() ){
        //QLineEdit* edit = qobject_cast<QLineEdit*>(QObject::sender());
        //qDebug() << "Valor: " << str.toInt();
        int valor = str.toInt();
        QRegExp rx("lineEdit_(.)_(.)");
        rx.indexIn(this->objectName());
        int fila = rx.cap(1).toInt();
        int columna = rx.cap(2).toInt();
        //qDebug() << " sender " << this->objectName();


            //qDebug() << matriz[7][1];
            if (isRepeated(fila,columna,valor)){
                qDebug() << "Fila:" << fila << " Columna:" << columna << " Valor:" << valor;
                 //qDebug() << matriz[fila][columna];
                qDebug() << "Repetido!";
                QString * q = new QString("");
                q->clear();
                matriz[fila][columna]->setText(*q);
                if (av){
                        av->show();
                 }
            }

    }
}
